package org.ainlolcat.ainscience.UI;


import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.tools;

import javax.swing.*;
import java.awt.*;
import java.util.Hashtable;

/**
 * @author ainlolcat
 * Date: 10/29/11
 */
public class MainWindow extends JFrame {

    private static MainWindow mainWindow;
    private int width;

    private boolean panes_inited = false;
    private JTabbedPane leftPane;
    private XListPane entities;
    private XListPane metaEntities;
    private XListPane templates;
    private static String TAB_TEMPLATES="Templates";
    private static String TAB_ENTITIES ="Entities";
    private static String TAB_META_ENTITIES ="Meta Entities";
    private XTabbedPane centralPane;
    private Hashtable<String, Integer> centralPaneLinks;

    private MainWindow(){
        super();
        init();
    }

    public static MainWindow getInstance(){
        if (mainWindow==null) mainWindow = new MainWindow();
        return mainWindow;
    }

    private void init(){
        setLayout(new BorderLayout(10,10));
        setSize(tools.getScreenSize());
        width = getWidth();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(new AppPanel(), BorderLayout.PAGE_START);

        initPanes();
        initContent();
    }

    private void initPanes(){
        initLeftPane();
        initCentralPane();
    }

    private void initLeftPane(){
        leftPane = new JTabbedPane();
        leftPane.setPreferredSize(new Dimension(200,this.getHeight()));
        add(leftPane, BorderLayout.LINE_START);
    }

    private void initCentralPane(){
        centralPane = new XTabbedPane();
        centralPaneLinks = new Hashtable<String, Integer>();
        add(centralPane, BorderLayout.CENTER);
    }

    private void addLeftPane(String name, Component pane){
        leftPane.add(name,pane);
        setVisible(true);
    }

    private void addCentralPane(Integer id, String name, JPanel pane){
        centralPane.add(name,pane);
        centralPaneLinks.put(name,id);
        setVisible(true);
    }

    private void initContent(){
        //initTemplateList();
        initEntriesList();
        //initMetaEntriesList();
    }

    private  void initTemplateList(){
        templates = new XListPane(Controller.getInstance().getProject().getTemplateNames());
        addLeftPane(TAB_TEMPLATES,templates);
    }

    private  void initEntriesList(){
        //entities = new XListPane(Controller.getInstance().getProject().getEntityNames());
        entities = new XListPane(new EntityListModel());
        entities.addSelectedValueListener(new EntitySelectedValueListener());
        addLeftPane(TAB_ENTITIES,entities);
    }

    private  void initMetaEntriesList(){
        metaEntities = new XListPane(Controller.getInstance().getProject().getMetaEntityNames());
        addLeftPane(TAB_META_ENTITIES,new JScrollPane(metaEntities));
    }

    //show function block
    public void showEntity(Entity entity){
        addCentralPane(entity.getID(), entity.getName(),entity.getRenderer());
    }

    //update function list. No add function anymore
    public void updateContent(){
        if (!panes_inited){
            initPanes();
            initContent();
            panes_inited = true;
        }
        updateEntriesList();
        updateMetaEntriesList();
        updateTemplatesList();
    }

    public void updateEntriesList(){
        entities.setListData(Controller.getInstance().getProject().getEntityNames());
        validateTree();
        repaint();
    }

    public void updateMetaEntriesList(){
        metaEntities.setListData(Controller.getInstance().getProject().getMetaEntityNames());
        validateTree();
        repaint();
    }

    public void updateTemplatesList(){
        templates.setListData(Controller.getInstance().getProject().getTemplateNames());
        validateTree();
        repaint();
    }

}
